package com.htfyun.ft.agingtest;

import android.app.Activity;
import android.content.Context;
import android.os.Message;
import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.htfyun.ft.FTApplication;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.TimeStringUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

/**
 * Created by sst on 2018/10/30.
 */

public abstract class AgingTestItemBase implements Comparable<AgingTestItemBase>{

    private AgingTestItemHandler handler ;

    private volatile boolean isTestStarted = false;
    private final Object lock = new Object();

    private Context context;

    private ViewGroup rootView;

    public AgingTestItemBase() {
        handler = new AgingTestItemHandler(this);
    }

    public void setContext(Context context) {
        this.context = context;

        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (needKeepScreenOn()) {
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            } else {
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
        }
    }

    @NonNull
    public Context getContext() {
        if (context == null) {
            context = FTApplication.getContext();
        }
        return context;
    }

    protected String getString(@StringRes int resId) {
        return getContext().getString(resId);
    }

    protected String getString(@StringRes int resId, Object... formatArgs) {
        return getContext().getString(resId, formatArgs);
    }

    public void setRootView(ViewGroup rootView) {
        this.rootView = rootView;
    }

    @Nullable
    public ViewGroup getRootView() {
        return rootView;
    }

    public boolean isTestStarted() {

        synchronized (lock) {
            return isTestStarted;
        }

    }

    protected void setTestStarted(boolean started) {
        this.isTestStarted = started;
    }

    public void startTest() {

        synchronized (lock) {
            if (isTestStarted()) {
                return;
            }

            setTestStarted(true);
        }

        setItemStartTime();

        doStartTest();
    }

    public void stopTest() {

        synchronized (lock) {
            if (!isTestStarted()) {
                return;
            }

            setTestStarted(false);

        }

        setItemExitTime();

        doStopTest();
    }

    public void clearRecord() {
        if (getAgingTestItemAttr() != null) {
            getAgingTestItemAttr().clear();
        }
    }

   @NonNull
   protected AgingTestItemHandler getHandler() {
        return handler;
    }


    @Override
    public int compareTo(AgingTestItemBase o) {
        if (this.getAgingTestItemAttr() == null) {
            return 0;
        }
        if (o.getAgingTestItemAttr() == null) {
            return 0;
        }

        return this.getAgingTestItemAttr().getId() - o.getAgingTestItemAttr().getId();
    }

    @NonNull
    @Override
    public String toString() {
        if (getAgingTestItemAttr() != null) {
            return getAgingTestItemAttr().getName();
        }
        return super.toString();
    }

    private void setItemStartTime() {
        if (getAgingTestItemAttr() == null) {
            return;
        }

        if (getAgingTestItemAttr().getStartTime_ms() <= 0
                || !AgingBooleanSettingsAttr.keep_record.get()) {

            long tick = AgingLongSettingsAttr.aging_time_tick.get();
            String nowStr = TimeStringUtils.makeTimeString(tick);

            DebugLog.e("setItemStartTime -> item = " + getAgingTestItemAttr().getNominalName() + ", nowStr = " + nowStr);

            getAgingTestItemAttr().setStartTime_ms(tick);
            getAgingTestItemAttr().setStartTimeAbsolute_ms(System.currentTimeMillis());

        }

    }

    private void setItemExitTime() {
        if (getAgingTestItemAttr() == null) {
            return;
        }

        long tick = AgingLongSettingsAttr.aging_time_tick.get();
        String nowStr = TimeStringUtils.makeTimeString(tick);

        DebugLog.e("setItemExitTime -> item = " + getAgingTestItemAttr().getNominalName() + ", nowStr = " + nowStr);

        getAgingTestItemAttr().setExitTime_ms(tick);
        getAgingTestItemAttr().setExitTimeAbsolute_ms(System.currentTimeMillis());

    }


    protected abstract void doStartTest();

    protected abstract void doStopTest();

    public abstract AgingTestItemAttr getAgingTestItemAttr();

    public abstract CharSequence getInfoString();

    protected boolean needKeepScreenOn(){
        return true;
    }
    protected void handleMessage(Message msg) {

    };

}
